<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树形结构 -->
      <!-- 树形组件 -->
      <el-tree
        :data="depts"
        :props="defaultProps"
        :expand-on-click-node="false"
        default-expand-all
      >
        <span slot-scope="{ node,data}" class="custom-tree-node">
          <span>{{ node.label }}</span>
          <span>
            <span class="tree-manager">{{ data.managerName }}</span>
            <!--显示区域内容-->
            <!--$event-->
            <el-dropdown @command="operateDept($event,data.id)">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right"/>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑子部门</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
        </span>
      </el-tree>

    </div>
    <!--放置弹层-->
    <!--.sync表示接受子组件的事件，update:showDialog 值==>属性-->
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :depts="depts[0]['children']" :current-node-id="currentNodeId" @updateDepartment="getDepartment"v-if="flag"></add-dept>
  </div>
</template>
<script>
import {delDepartment, getDepartment} from "@/api/department";
import {transLisToTreeData} from "@/utils";
import AddDept from "@/views/department/components/add-dept.vue";

export default {
  name: 'Department',
  components: {AddDept},
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      flag: false,
      currentNodeId:null// 当前节点
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    //获取组织架构数据
    async getDepartment() {
      console.log('import request from \'@/utils/request\'')
      const result = await getDepartment()
      console.log(result)
      this.flag = true
      this.depts = transLisToTreeData(result, 0)
    },
    // 操作部门的方法
    operateDept(type,id) {
      console.log(type,id)
      if (type=== 'add') {
        this.showDialog = true
        this.currentNodeId=id
      }else if(type=== 'edit'){
        this.showDialog=true
        this.currentNodeId = id
        this.$nextTick(()=>{
          this.$refs.addDept.getDepartmentDetail(this.currentNodeId)
        })

      }else {
        // 删除部门
        this.$confirm('您确认要删除该部门吗','提示',{
          confirmButtonText:'确定',
          cancelButtonText:"取消",
          closeOnClickModal:false,
          type:'warning',
          // center:true
        }).then(async () => {
          await delDepartment(id)
          // 提示消息
          this.$message.success('删除部门成功')
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 20px;
}

.el-dropdown {
  margin-left: 10px;
}


</style>
<style>
.el-message-box__btns {
  padding: 5px 15px 0;
  display: flex;
  flex-direction: row-reverse;
  gap: 10px;
}
</style>
